package com.supplychain.scm.core.rocketmq.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.supplychain.scm.core.common.jackson.DbyJavaTimeModule;
import org.apache.rocketmq.spring.support.RocketMQMessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.CompositeMessageConverter;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.converter.MessageConverter;

import java.util.List;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName RocketMQMessageConverterConfig
 * @createTime 13:17:00 2023/04/07
 */
@Configuration
public class RocketMQMessageConverterConfig {

    @Bean
    @ConditionalOnMissingBean
    public RocketMQMessageConverter createRocketMQMessageConverter() {
        RocketMQMessageConverter converter = new RocketMQMessageConverter();
        CompositeMessageConverter compositeMessageConverter = (CompositeMessageConverter) converter.getMessageConverter();
        List<MessageConverter> messageConverterList = compositeMessageConverter.getConverters();
        for (MessageConverter messageConverter : messageConverterList)
        {
            if (messageConverter instanceof MappingJackson2MessageConverter) {
                MappingJackson2MessageConverter jackson2MessageConverter = (MappingJackson2MessageConverter) messageConverter;
                ObjectMapper objectMapper = jackson2MessageConverter.getObjectMapper();
                DbyJavaTimeModule module = new DbyJavaTimeModule();
                objectMapper.registerModules(module);
                break; 
            }
        }
        return converter;
    }
}
